<?php 
class App_Misc
{
	static public function make_dir($folder)
	{
	    $reval = false;

	    if (!file_exists($folder)){
	        /* 如果目录不存在则尝试创建该目录 */
	        @umask(0);

	        /* 将目录路径拆分成数组 */
	        preg_match_all('/([^\/]*)\/?/i', $folder, $atmp);

	        /* 如果第一个字符为/则当作物理路径处理 */
	        $base = ($atmp[0][0] == '/') ? '/' : '';

	        /* 遍历包含路径信息的数组 */
	        foreach ($atmp[1] AS $val)
	        {
	            if ('' != $val){
	                $base .= $val;

	                if ('..' == $val || '.' == $val){
	                    /* 如果目录为.或者..则直接补/继续下一个循环 */
	                    $base .= '/';

	                    continue;
	                }
	            }
	            else{
	                continue;
	            }

	            $base .= '/';

	            if (!file_exists($base)) {
	                /* 尝试创建目录，如果创建失败则继续循环 */
	                if (@mkdir(rtrim($base, '/'), 0777)) {
	                    @chmod($base, 0777);
	                    $reval = true;
	                }
	            }
	        }
	    }else{
		        /* 路径已经存在。返回该路径是不是一个目录 */
		        $reval = is_dir($folder);
		    }
		 clearstatcache();
		 return $reval;
	}
	
	static public function move_upload_file($file_name, $target_name = '')
	{
	    if (function_exists("move_uploaded_file")){
	        if (move_uploaded_file($file_name, $target_name)){
	            @chmod($target_name,0755);
	            return true;
	        } else if (copy($file_name, $target_name)){
	            @chmod($target_name,0755);
	            return true;
	        }
	    } elseif (copy($file_name, $target_name)) {
	        @chmod($target_name,0755);
	        return true;
	    }
	    return false;
	}
	
	static public function getIp()
	{
	    static $realip = NULL;

        if ($realip !== NULL) {
            return $realip;
        }
    
        if (isset($_SERVER)) {
            if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                
                $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);    
                
                foreach ($arr AS $ip) {
                    $ip = trim($ip);    
                    if ($ip != 'unknown') {
                        $realip = $ip;
                        break;
                    }
                }
            } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
                $realip = $_SERVER['HTTP_CLIENT_IP'];
            } else {
                if (isset($_SERVER['REMOTE_ADDR'])) {
                    $realip = $_SERVER['REMOTE_ADDR'];
                } else {
                    $realip = '0.0.0.0';
                }
            }
        } else {
            if (getenv('HTTP_X_FORWARDED_FOR')) {
                $realip = getenv('HTTP_X_FORWARDED_FOR');
            } elseif (getenv('HTTP_CLIENT_IP')) {
                $realip = getenv('HTTP_CLIENT_IP');
            } else {
                $realip = getenv('REMOTE_ADDR');
            }
        }
        preg_match("/[\d\.]{7,15}/", $realip, $onlineip);
        $realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';
        return $realip;
	}
	
	static public function showMsg($msg)
	{
	    echo '<ul>';
	    if (is_array($msg)) {
	        foreach ($msg as $v) {
	            echo '<li style="color: red;">' . $v . '</li>';
	        }
	    } else {
	        echo '<li style="color: red;">' . $msg . '</li>';
	    }
	    echo '</ul>';
	}
	
	static public function writeLog($opt)
	{
	    $logModel = App_Model::factory('Log');
	    
	    $row = array('opt'=>$opt,
    	             'ip'=>self::getIp(),
    	             'add_time'=>date('Y-m-d H:i:s', time())
    	            );
    	            	    
	    if ($user = App_User::getCurrentUser()) {
    	    $row['user_id'] = $user->user_id;
    	    $row['user_name'] = $user->user_name;
	    }
	    
	    return $logModel->add($row);
	}
	
	static public function page($link, $page_count, $current_page, $param, $half_per)
	{
		$page = '';
		$page_ext = '';
		$pageExtArr = array();
		
		foreach ($param as $k=>$v) {
			$pageExtArr[] = $k . '=' . $v;
		}
		$page_ext = implode('&', $pageExtArr);
		
	    $page .= ($current_page > 1 ) ? "<li><a href=\"$link?page=" . ($current_page-1) ."&$page_ext\" rel=\"nofollow\">Prev</a></li>" : '';
	    for ($i = $current_page - $half_per,$i >= 1 || $i = 1 ,$j = $current_page + $half_per, $j < $page_count || $j = $page_count;$i <= $j ;$i++ ){
	        $nofollow = ($i != 1) ? ' rel="nofollow"' : '';
	    	$page .= ($i == $current_page) ? "<li class='current'><a href=\"$link?page=$i&$page_ext\"$nofollow>$i</a></li>" : "<li class='p_li'><a href=\"$link?page=$i&$page_ext\"$nofollow>$i</a></li>";
	    }
	    $page .= ($current_page < $page_count) ? "<li><a href=\"$link?page=$page_count&$page_ext\" rel=\"nofollow\">Last</a></li>":'';
	    return $page;
	}
}
